﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Threading;
using System.Windows.Forms;
using DM_Face_Detecion_In_Image.Translations;

namespace DM_Face_Detecion_In_Image
{
    public partial class Form1 : Form
    {
        private readonly ResourceManager manager;

        public Form1()
        {
            InitializeComponent();
            manager = english.ResourceManager;
            init(manager);
        }

        private void init(ResourceManager mng)
        {
            btnDetect.Text = mng.GetString("Detect");
            btnLoad.Text = mng.GetString("LoadImage");
            btnSave.Text = mng.GetString("SaveImage");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Filter =
                "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|BMP Files(*.bmp)|*.bmp|All Files|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = dialog.FileName;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            pbImage.Image = Image.FromFile(txtPath.Text);
            pbImage.Image.Save("img.jpg");
            btnDetect.Enabled = true;
        }

        private void btnDetect_Click(object sender, EventArgs e)
        {
            btnDetect.Enabled = false;
            btnSave.Enabled = true;
            var commandToExecute = @" Detection.exe img.jpg";
            Process.Start("cmd", "/c" + @commandToExecute);
            Thread.Sleep(5000);
            pbImage.Image = Image.FromFile("Image.jpg");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            File.Delete("img.jpg");
            pbImage.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var dialog = new SaveFileDialog();
            dialog.Filter =
                "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|BMP Files(*.bmp)|*.bmp|All Files|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                pbImage.Image.Save(dialog.FileName);
                btnSave.Enabled = false;
            }
        }
    }
}